﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;

namespace WTSQCB
{
    public partial class Lop : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        CultureInfo viVN = new CultureInfo("vi-VN");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
                NgaySinh_Load();
        }

        protected void ThemButton1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "LopThem";
                cmd.Parameters.AddWithValue("@malop", MaLopTextBox1.Text);
                cmd.Parameters.AddWithValue("@tenlop", TenLopTextBox2.Text);
                cmd.Parameters.AddWithValue("@khoahoc", KhoaHocTextBox3.Text);
                cmd.Parameters.AddWithValue("@namthanhlap", DateTime.Parse(ddlNam.SelectedValue + "/" + ddlThang.SelectedValue + "/" + ddlNgay.SelectedValue));                                
                cmd.Parameters.AddWithValue("@mahedaotao", HeDaoTaoDropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@matrinhdo", TrinhDoDropDownList2.SelectedValue);
                cmd.Parameters.AddWithValue("@machuyennganh", ChuyenNganhDropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@madaidoi", DaiDoiDropDownList1.SelectedValue);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                LopGridView1.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void SuaButton2_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "LopSua";
                cmd.Parameters.AddWithValue("@malop", MaLopTextBox1.Text);
                cmd.Parameters.AddWithValue("@tenlop", TenLopTextBox2.Text);
                cmd.Parameters.AddWithValue("@khoahoc", KhoaHocTextBox3.Text);                
                cmd.Parameters.AddWithValue("@namthanhlap", DateTime.Parse(ddlNam.SelectedValue + "/" + ddlThang.SelectedValue + "/" + ddlNgay.SelectedValue));                
                cmd.Parameters.AddWithValue("@mahedaotao", HeDaoTaoDropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@matrinhdo", TrinhDoDropDownList2.SelectedValue);
                cmd.Parameters.AddWithValue("@machuyennganh", ChuyenNganhDropDownList1.SelectedValue);
                cmd.Parameters.AddWithValue("@madaidoi", DaiDoiDropDownList1.SelectedValue);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                LopGridView1.DataBind();
                Response.Write("Sửa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void XoaButton3_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "LopXoa";
                cmd.Parameters.AddWithValue("@malop", MaLopTextBox1.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                LopGridView1.DataBind();
                Response.Write("Xóa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow dong = LopGridView1.SelectedRow;
            MaLopTextBox1.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenLopTextBox2.Text = HttpUtility.HtmlDecode(dong.Cells[2].Text);
            KhoaHocTextBox3.Text = HttpUtility.HtmlDecode(dong.Cells[3].Text);
            ddlNgay.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[4].Text)).Day.ToString();
            ddlThang.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[4].Text)).Month.ToString();
            ddlNam.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[4].Text)).Year.ToString();

        }
        protected void NgaySinh_Load()
        {
            int i;
            for (i = 1990; i < 2030; i++)
            {
                ddlNam.Items.Add(i.ToString());
            }

            for (i = 1; i < 13; i++)
            {
                ddlThang.Items.Add(i.ToString());
            }

            for (i = 1; i < 32; i++)
            {
                ddlNgay.Items.Add(i.ToString());
            }
        }
        protected void ThongBao(string msg = "Thực hiện thành công")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');", true);
        }
    }
}